package com.meizi.experimentcode.awtswing;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;

/**
 * @author Administration
 * @date 2022/8/17 - 10:55 - 星期三
 * @project lambda
 * @package com.meizi.experimentcode.awtswing
 * @by 晨
 */
public class JFrameDemo2 {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("JFrameDemo2");
        JButton button = new JButton("Press me");
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new BorderLayout());
        jPanel.add(button, BorderLayout.CENTER);
        jFrame.setContentPane(jPanel);
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class FrameWithPanel{
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("FrameWithPanel");
        Container contentPane = jFrame.getContentPane();
        contentPane.setBackground(Color.CYAN);
        JPanel jPanel = new JPanel();
        jPanel.setBackground(Color.YELLOW);
        JButton button = new JButton("Press me");
        jPanel.add(button);
        contentPane.add(jPanel, BorderLayout.CENTER);
        jFrame.setSize(300,200);
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


class JButtonExample extends WindowAdapter implements ActionListener {
    JFrame f;
    JButton b;
    JTextField tf;
    int tag = 0;

    public static void main(String[] args) {
        JButtonExample jButtonExample = new JButtonExample();
        jButtonExample.go();
    }

    private void go() {
        f = new JFrame("JButton Example");
        b = new JButton("sample");
        b.addActionListener(this);
        f.getContentPane().add(b,"South");
        tf = new JTextField();
        f.getContentPane().add(tf,"Center");
        f.addWindowListener(this);
        f.setSize(300,150);
        f.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String s1 = "123456";
        String s2 = "123456789";
        if (tag == 0) {
            tf.setText(s1); tag = 1;
        }else {
            tf.setText(s2); tag = 0;
        }
    }

    @Override
    public void windowClosing (WindowEvent e) {
        System.exit(0);
    }
}


class FlowLayoutDame{
    private JFrame frame;
    private JButton button1, button2, button3;

    public static void main(String[] args) {
        FlowLayoutDame flowLayoutDame = new FlowLayoutDame();
        flowLayoutDame.go();
    }

    private void go() {
        frame = new JFrame("Flow Layout");
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new FlowLayout());
        button1 = new JButton("ok");
        button2 = new JButton("open");
        button3 = new JButton("close");
        contentPane.add(button1);
        contentPane.add(button2);
        contentPane.add(button3);
        frame.setSize(200,100);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class CardLayoutDemo extends MouseAdapter{
    JPanel p1, p2, p3;
    JLabel l1, l2, l3;
    CardLayout cardLayout;
    JFrame frame;
    Container container;

    public static void main(String[] args) {
        CardLayoutDemo cardLayoutDemo = new CardLayoutDemo();
        cardLayoutDemo.go();
    }

    private void go() {
        frame = new JFrame("Card Test");
        container = frame.getContentPane();
        cardLayout = new CardLayout();
        container.setLayout(cardLayout);
        p1 = new JPanel();
        p2 = new JPanel();
        p3 = new JPanel();
        l1 = new JLabel("123");
        p1.add(l1);
        p1.setBackground(Color.yellow);
        l2 = new JLabel("456");
        p2.add(l2);
        p2.setBackground(Color.green);
        l3 = new JLabel("789");
        p3.add(l3);
        p3.setBackground(Color.magenta);
        p1.addMouseListener(this);
        p2.addMouseListener(this);
        p3.addMouseListener(this);

        container.add(p1, "First");
        container.add(p2, "Second");
        container.add(p3, "Third");
        cardLayout.show(container, "First");
        frame.setSize(300,200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void mouseClicked (MouseEvent event) {
        cardLayout.next(container);
    }
}


class NullLayoutDemo {
    private JFrame frame;
    private JButton b1, b2, b3;

    public static void main(String[] args) {
        NullLayoutDemo nullLayoutDemo = new NullLayoutDemo();
        nullLayoutDemo.go();
    }

    private void go() {
        frame = new JFrame("NullLayoutDemo");
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(null);
        b1 = new JButton("yes");
        contentPane.add(b1);
        b2 = new JButton("no");
        contentPane.add(b2);
        b3 = new JButton("cancel");
        contentPane.add(b3);
        b1.setBounds(30,25,75,20);
        b2.setBounds(60,60,75,50);
        b3.setBounds(160,20,75,30);
        frame.setSize(300,200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}


class ActionEventDemo {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("ActionEventDemo");
        JButton button = new JButton("Press me");
        button.addActionListener(new ButtonHandler());
        jFrame.getContentPane().add(button, BorderLayout.CENTER);
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
class ButtonHandler implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Action occurred");
    }
}


class TwoListener implements MouseMotionListener, MouseListener{

    private JFrame frame;
    private JTextField tf;

    public static void main(String[] args) {
        TwoListener twoListener = new TwoListener();
        twoListener.go();
    }

    private void go() {
        frame = new JFrame("TwoListener");
        Container contentPane = frame.getContentPane();
        contentPane.add(new Label("Click and drag the mouse"), BorderLayout.NORTH);
        tf = new JTextField(30);
        contentPane.add(tf, BorderLayout.SOUTH);
        frame.addMouseMotionListener(this);
        frame.addMouseListener(this);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        String s = "1234567";
        tf.setText(s);
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {
        String s = "0000000000000000";
        tf.setText(s);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        String s = "X=" + e.getX() + "Y=" +e.getY();
        tf.setText(s);
    }

    @Override
    public void mouseMoved(MouseEvent e) {

    }
}


class MyFrame123 {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("MyFrame");
        Container contentPane = jFrame.getContentPane();
        contentPane.setLayout(new FlowLayout());
        contentPane.setBackground(Color.RED);
        JPanel jPanel = new JPanel();
        jPanel.setBackground(Color.YELLOW);
        JButton button1 = new JButton("打开");
        JButton button2 = new JButton("关闭");
        JButton button3 = new JButton("返回");
        jPanel.add(button1);
        jPanel.add(button2);
        jPanel.add(button3);
        contentPane.add(jPanel);
        jFrame.setSize(600,300);
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

class MyFrame4  {
    public static void main(String[] args) {
        JFrame jFrame = new JFrame("MyFrame");
        Container contentPane = jFrame.getContentPane();
        contentPane.setLayout(new BorderLayout());
        JButton button1 = new JButton("上");
        JButton button2 = new JButton("中");
        JButton button3 = new JButton("下");
        contentPane.add(button1,BorderLayout.NORTH);
        contentPane.add(button2,BorderLayout.CENTER);
        contentPane.add(button3,BorderLayout.SOUTH);
        jFrame.setSize(300,600);
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

class JComboBoxDemo {
    JFrame frame = new JFrame("JComboBoxDemo");
    JComboBox<String> jcb1, jcb2;
    JTextArea ta = new JTextArea(0,30);

    public static void main(String[] args) {
        JComboBoxDemo jComboBoxDemo = new JComboBoxDemo();
        jComboBoxDemo.go();
    }

    private void go() {
        JPanel p1 = new JPanel();
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();
        JPanel p4 = new JPanel();
        String[] item = {"1", "2", "3", "4", "5", "6"};
        jcb1 = new JComboBox<String>(item);
        p1.add(jcb1);
        jcb1.setSelectedIndex(3);
        Border border = BorderFactory.createEtchedBorder();
        TitledBorder titledBorder = BorderFactory.createTitledBorder(border, "Box");
        p1.setBorder(titledBorder);

        jcb2 = new JComboBox<String>();
        jcb2.addItem("7");
        jcb2.addItem("8");
        jcb2.addItem("9");
        jcb2.addItem("10");
        jcb2.setEditable(true);
        p2.add(jcb2);
        titledBorder = BorderFactory.createTitledBorder(border,"Editable JComBox");
        p2.setBorder(titledBorder);

        JScrollPane jScrollPane = new JScrollPane();
        p3.setLayout(new BorderLayout());
        titledBorder = BorderFactory.createTitledBorder(border,"etched JComBox");
        p3.setBorder(titledBorder);
        ActionListener actionListener = new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent e) {
                JComboBox jcb = (JComboBox)e.getSource();
                if (jcb == jcb1) {
                    jcb2.insertItemAt((String)jcb1.getSelectedItem(),0);
                    ta.append("\nItem " + jcb1.getSelectedItem() + " insetted");
                } else {
                    jcb2.addItem((String)jcb2.getSelectedItem());
                }
            }
        };
        jcb1.addActionListener(actionListener);
        jcb2.addActionListener(actionListener);
        p4.setLayout(new GridLayout(0,1));
        p4.add(p1);
        p4.add(p2);
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(0,1));
        contentPane.add(p4);
        contentPane.add(p3);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}

class JTextAreaDemo {
    JFrame frame = new JFrame("JTextAreaDemo");
    JTextArea ta1, ta2;
    JButton copy, clear;

    public static void main(String[] args) {
        JTextAreaDemo jTextAreaDemo = new JTextAreaDemo();
        jTextAreaDemo.go();
    }

    private void go() {
        ta1 = new JTextArea(3,15);// 3行15列
        ta1.setSelectionColor(Color.red);
        ta2 = new JTextArea(7, 20);
        ta2.setEnabled(false); // 设置为不可编辑的
        JScrollPane jsp1 = new JScrollPane(ta1, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        JScrollPane jsp2 = new JScrollPane(ta2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        copy = new JButton("Copy");
        copy.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (ta1.getSelectedText() != null) {
                    ta2.append(ta1.getSelectedText() + "\n");
                } else {
                    ta2.append("\n" +  ta1.getText() + "\n");
                }
            }
        });
        clear = new JButton("Clear");
        clear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ta2.setText("");
            }
        });

        JPanel jPanel = new JPanel();
        JPanel jPane2 = new JPanel();
        Border etchedBorder = BorderFactory.createEtchedBorder();
        Border titledBorder1 = BorderFactory.createTitledBorder(etchedBorder, "输入区");
        Border titledBorder2 = BorderFactory.createTitledBorder(etchedBorder, "复制区");
        jPanel.setBorder(titledBorder1);
        jPane2.setBorder(titledBorder2);
        jPanel.add(jsp1);
        jPanel.add(copy);
        jPane2.add(jsp2);
        jPane2.add(clear);
        Container contentPane = frame.getContentPane();
        contentPane.add(jPanel,BorderLayout.CENTER);
        contentPane.add(jPane2, BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}